<template>
  <div id="aCoursesList" class="bg-fa of">
    <div id="login" v-if="loginInfo === ''">
      <p class="fsize16" style="  display: inline-block;">您还没有登录！请先登录后再观看购买</p>
      <el-button type="danger" @click="loginAfterPay" size="mini" style="margin-left: 20px">立即登录</el-button>
    </div>
    <!-- /课程详情 开始 -->
    <section class="container">
      <section class="path-wrap txtOf hLh30">
        <a href="/" title class="c-999 fsize14">首页</a>
        \
        <a href="" title class="c-999 fsize14">{{ courseInfo.subjectParentTitle }}</a>
        \
        <span class="c-333 fsize14">{{ courseInfo.subjectTitle }}</span>
      </section>
      <div>
        <article class="c-v-pic-wrap" style="height: 357px;">
<!--          <section class="p-h-video-box" id="videoPlay">-->
            <!--            <img :src="courseInfo.cover" :alt="courseInfo.title" class="dis c-v-pic">-->
<!--            <el-image-->
<!--              style="width: 100%; height: 100%"-->
<!--              :src="courseInfo.cover"-->
<!--              :fit="'fill'"></el-image>-->
          <el-image style="width: 100%; height: 100%" :src="src"></el-image>
<!--          </section>-->
        </article>
        <aside class="c-attr-wrap">
          <section class="ml20 mr15">
            <h2 class="hLh30 txtOf mt15">
              <span class="c-fff fsize24">{{ courseInfo.title }}</span>
            </h2>
            <section class="c-attr-jg">
              <span class="c-fff">价格：</span>
              <b class="c-yellow" style="font-size:24px;">￥{{ courseInfo.price }}</b>
            </section>
            <section class="c-attr-mt c-attr-undis">
              <span class="c-fff fsize14">主讲： {{ courseInfo.teacherName }}&nbsp;&nbsp;&nbsp;</span>
            </section>
            <section class="c-attr-mt of">
              <span class="ml10 vam">
                <em class="icon18 scIcon"></em>
                <a class="c-fff vam" title="收藏" href="#">收藏</a>
              </span>
            </section>
            <section class="c-attr-mt" v-if="courseInfo.price === 0 || isBuy">
<!--              <a href="#" class="comm-btn c-btn-3">立即观看</a>-->
              <el-button type="danger" round style="width: 260px; height: 50px" @click="LookVideo('#lh-menu')">立即观看</el-button>
            </section>
            <section class="c-attr-mt" v-else>
<!--              <a class="comm-btn c-btn-3" @click="isLogin">立即购买</a>-->
              <el-button type="danger" round style="width: 260px; height: 50px" @click="isLogin()">立即购买</el-button>
            </section>
          </section>
        </aside>
        <aside class="thr-attr-box">
          <ol class="thr-attr-ol clearfix">
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">购买数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseInfo.buyCount }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">课时数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseInfo.lessonNum }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">浏览数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseInfo.viewCount }}</h6>
              </aside>
            </li>
          </ol>
        </aside>
        <div class="clear"></div>
      </div>
      <!-- /课程封面介绍 -->
      <div class="mt20 c-infor-box">
        <article class="fl col-7">
          <section class="mr30">
            <div class="i-box">
              <div>
                <section id="c-i-tabTitle" class="c-infor-tabTitle c-tab-title">
                  <a name="c-i" class="current" title="课程详情">课程详情</a>
                </section>
              </div>
              <article class="ml10 mr10 pt20">
                <div>
                  <h6 class="c-i-content c-infor-title">
                    <span>课程介绍</span>
                  </h6>
                  <div class="course-txt-body-wrap">
                    <section class="course-txt-body">
                      <p v-html="courseInfo.description">{{ courseInfo.description }}</p>
                    </section>
                  </div>
                </div>
                <!-- /课程介绍 -->
                <div class="mt50" id="tree">
                  <h6 class="c-g-content c-infor-title">
                    <span>课程大纲</span>
                  </h6>
                  <section class="mt20">
                    <div class="lh-menu-wrap">
                      <menu id="lh-menu" class="lh-menu mt10 mr10">
                        <ul>
                          <!-- 文件目录 -->
                          <el-tree
                            :data="chapterVideoList"
                            :props="defaultProps"
                            highlight-current="true">
                            <span class="custom-tree-node" slot-scope="{ node, data }">
                              <span>{{ node.label }}</span>
                              <span>
                                <el-button
                                  type="text"
                                  size="mini"
                                  style="font-size: 16px"
                                  @click="freeVideo(node.data.videoSourceId)"
                                  v-if="!node.parent.data.length > 0 && node.data.isFree && !isBuy">
                                  免费观看
                                </el-button>

                                <el-button
                                  type="text"
                                  size="mini"
                                  style="font-size: 16px"
                                  @click="freeVideo(node.data.videoSourceId)"
                                  v-else-if="!node.parent.data.length > 0 && isBuy">
                                  立即观看
                                </el-button>
                              </span>
                            </span>
                          </el-tree>
                        </ul>
                      </menu>
                    </div>
                  </section>
                </div>
                <!-- /课程大纲 -->
              </article>
            </div>
          </section>
        </article>
        <aside class="fl col-3">
          <div class="i-box">
            <div>
              <section class="c-infor-tabTitle c-tab-title">
                <a title href="javascript:void(0)">主讲讲师</a>
              </section>
              <section class="stud-act-list">
                <ul style="height: auto;">
                  <li>
                    <div class="u-face">
                      <a :href="'/teacher/' + courseInfo.id">
                        <el-avatar shape="circle" :size="60" :src="courseInfo.avatar"></el-avatar>
                        <!--                        <img :src="courseInfo.avatar" width="50" height="50" :alt="courseInfo.name">-->
                      </a>
                    </div>
                    <section class="hLh30 txtOf">
                      <a class="c-333 fsize16 fl" :href="'/teacher/' + courseInfo.id">{{ courseInfo.teacherName }}</a>
                    </section>
                    <section class="hLh20 txtOf">
                      <span class="c-999">{{ courseInfo.career }}</span>
                    </section>
                  </li>
                </ul>
              </section>
            </div>
          </div>
        </aside>
        <div class="clear"></div>
      </div>
    </section>
    <!-- /课程详情 结束 -->
  </div>
</template>

<script>
import course from "@/api/course";
import login from "@/api/login";
import cookie from "js-cookie";

export default {
  data() {
    return {
      courseId: '',
      courseInfo: {},
      chapterVideoList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      isBuy: false,
      loginInfo: '',
      src: ''
    }
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      this.courseId = this.$route.params.id
    }
    if (this.$route.query.username !== undefined) {

    }
    this.showInfo()
    this.getCourseInfoById()
  },
  methods: {
    // 获取用户信息
    showInfo() {
      // 从cookie获取用户信息
      let userStr = cookie.get('guli_ucenter');
      // 把字符串转换为json对象
      if (userStr) {
        this.loginInfo = JSON.parse(userStr);
        this.initBuyInfo()
      }
    },
    initBuyInfo() {
      console.log(this.loginInfo.id)
      console.log(this.courseId)
      course.getBuyInfo(this.loginInfo.id, this.courseId)
        .then(res => {
          this.isBuy = res.data.data.isBuy
        })
    },
    // isLookVideo () {
    //
    // },
    getCourseInfoById() {
      course.getCourseInfoById(this.courseId)
        .then(res => {
          console.log(res.data.data)
          this.courseInfo = res.data.data.courseInfo
          this.src = this.courseInfo.cover
          this.chapterVideoList = res.data.data.chapterVideoList
        })
    },
    isLogin() {
      if (this.loginInfo === '') {
        this.$router.push({path: '/login/' + this.courseId})
      } else {
        this.$router.push({path: '/order/' + this.courseId})
      }
    },
    LookVideo(tree) {
      if (this.loginInfo === '') {
        this.$router.push({path: '/login/' + this.courseId})
      } else {
        this.$el.querySelector(tree).scrollIntoView({
          behavior: "smooth", // 平滑过渡
          block: "start" // 上边框与视窗顶部平齐
        });
      }
    },
    freeVideo(videoSourceId) {
      if (this.loginInfo === '') {
        this.$router.push({path: '/login/' + this.courseId})
      } else {
        window.open('/player/' + videoSourceId, '_blank');
      }
      // window.open('/player/' + videoSourceId, '_blank');
    }
  }
};
</script>
<style scoped>
#login {
  margin: auto;
  margin-top: 30px;
  padding: 10px;
  background-color: #fdfced;
  border: #dfd4a9 solid 2px;
  color: #d08747;
  width: 60%;
}
#lh-menu >>> .el-tree-node__content{
  font-size: 20px;
  height: 50px;
}
.fsize16 {
  font-size: 16px;
}
</style>
